package com.macro.mall.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.PmProdSpecStockTempMapper;
import com.macro.mall.mapper.PmServerSpecMapper;
import com.macro.mall.mapper.PmServerSpecPicMapper;
import com.macro.mall.model.PmServerSpec;
import com.macro.mall.model.PmServerSpecExample;
import com.macro.mall.model.PmServerSpecPic;
import com.macro.mall.model.PmServerSpecPicExample;
import com.macro.mall.product.constant.Constants;
import com.macro.mall.product.dto.PmServerSpecDto;
import com.macro.mall.product.service.PmServerSpecService;
import com.macro.mall.product.util.DozerUtils;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class PmServerSpecServiceImpl implements PmServerSpecService {
    @Autowired
    private PmServerSpecMapper pmServerSpecMapper;
    @Autowired
    private PmServerSpecPicMapper pmServerSpecPicMapper;
    @Autowired
    private PmProdSpecStockTempMapper pmProdSpecStockTempMapper;
    @Autowired
    private Mapper mapper;


    @Override
    public List<PmServerSpec> list(PmServerSpecDto param,boolean isNeedPic) {
        PmServerSpecExample example  = new PmServerSpecExample();
        PmServerSpecExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.STATUS_VALID);
        if(!StringUtils.isBlank(param.getServerSpecName())){
            criteria.andServerSpecNameLike("%"+param.getServerSpecName()+"%");
        }
        if(param.getParentId()!=null){
            criteria.andParentIdEqualTo(param.getParentId());
        }
        if(param.getServerSpecLevel() != null){
            criteria.andServerSpecLevelEqualTo(param.getServerSpecLevel() );
        }
        example.setOrderByClause(" sort asc ");
        List<PmServerSpec> serverSpecList = pmServerSpecMapper.selectByExample(example);
        if(isNeedPic && serverSpecList!=null && serverSpecList.size()>0){
            PmServerSpecPic pic ;
            for(PmServerSpec pmServerSpec : serverSpecList){
                pic = new PmServerSpecPic();
                pic.setServerSpecId(pmServerSpec.getId());
                pmServerSpec.setPicList(getPicList(pic));
            }
        }
        return serverSpecList;
    }
    @Override
    public List<PmServerSpec> listPage(int pageNum, int pageSize,PmServerSpecDto param,boolean isNeedPic) {
        PageHelper.startPage(pageNum, pageSize);
        List<PmServerSpec> serverSpecList = this.list(param,isNeedPic);
        return serverSpecList;
    }


    @Override
    public List<PmServerSpecDto> converDataList(List<PmServerSpec> serverSpecList) {

        if(serverSpecList!=null && serverSpecList.size()>0){
            for(PmServerSpec pmServerSpec : serverSpecList){
                converDataPic(pmServerSpec);
            }
        }
        List<PmServerSpecDto> dtoList = DozerUtils.mapList(mapper, serverSpecList, PmServerSpecDto.class);
        if(dtoList!=null && dtoList.size()>0){
            for(PmServerSpecDto dto : dtoList){
                PmServerSpecExample example  = new PmServerSpecExample();
                PmServerSpecExample.Criteria criteria = example.createCriteria();
                criteria.andStatusEqualTo(Constants.STATUS_VALID);
                criteria.andParentIdEqualTo(dto.getId());
                long  countDB = pmServerSpecMapper.countByExample(example);
                if(countDB ==0){
                    dto.setIsChildren("N");
                }else{
                    dto.setIsChildren("Y");
                }
            }
        }
        return dtoList;
    }
    @Override
    public PmServerSpecDto converDataInfo(PmServerSpec pmServerSpec) {
        converDataPic(pmServerSpec);
        PmServerSpecDto serverSpecDto= mapper.map(pmServerSpec,PmServerSpecDto.class);
        return serverSpecDto;
    }
    private void converDataPic(PmServerSpec pmServerSpec){
        Map<String,String> picMap  = new HashMap<String,String>();
        String picUri;
        if(pmServerSpec.getPicList()!=null && pmServerSpec.getPicList().size()>0){
            for(PmServerSpecPic pic: pmServerSpec.getPicList()){
                picUri = picMap.get(pic.getPicType());
                if(!StringUtils.isBlank(picUri)){
                    picUri=picUri+";"+pic.getPicId();
                }else{
                    picUri=pic.getPicId();
                }
                picMap.put(pic.getPicType(),picUri);
            }
        }
        /***业务代码*******************************************************************/
        String picMain = picMap.get(Constants.SERVER_SPEC_PIC_TYPE_MAIN);
        String picList = picMap.get(Constants.SERVER_SPEC_PIC_TYPE_LIST);
        if(!StringUtils.isBlank(picMain)){
            pmServerSpec.setPicMainStr(picMain);
        }
        if(!StringUtils.isBlank(picList)){
            pmServerSpec.setPicListStr(picList);
        }
    }
    @Override
    public int save(PmServerSpecDto serverSpecDto){
        int count =0;
        if(serverSpecDto!=null){
            /**1.数据转换******************/
            PmServerSpec serverSpec= mapper.map(serverSpecDto,PmServerSpec.class);
            Date curDate = new Date();

            //级别计算
            if(serverSpec.getParentId()!=null ) {
                if (serverSpec.getParentId().longValue() == 0){
                    serverSpec.setServerSpecLevel(1);
                }else{
                    PmServerSpec parent= pmServerSpecMapper.selectByPrimaryKey(serverSpec.getParentId());
                    if(parent!=null && parent.getServerSpecLevel()>0){
                        serverSpec.setServerSpecLevel(parent.getServerSpecLevel()+1);
                    }else{
                        serverSpec.setServerSpecLevel(1);
                    }
                }
            }else {
                serverSpec.setServerSpecLevel(1);
           }


            /**2.基础数据保存******************/
            if(serverSpec.getId()!=null && serverSpec.getId()>0){

               // serverSpec.setUpdateBy("xx");
                serverSpec.setUpdateTime(curDate);
                count= pmServerSpecMapper.updateByPrimaryKey(serverSpec);
            }else{
               // serverSpec.setUpdateBy("xx");
                serverSpec.setUpdateTime(curDate);
              //   serverSpec.setCreateBy("xx");
                serverSpec.setCreateTime(curDate);
                serverSpec.setStatus(Constants.STATUS_VALID);
                count= pmServerSpecMapper.insert(serverSpec);
            }
            /**3.图片保存******************/
            savePic(serverSpec);
        }
        return  count;
    }
    @Override
    public int delete(String serverSpecIds){
        int count =0;
        if(!StringUtils.isBlank(serverSpecIds)){
            String[] serverSpecIdArray =serverSpecIds.split(";");
            PmServerSpec pmServerSpec;
            Date curDate = new Date();
            for(String serverSpecId : serverSpecIdArray){
                pmServerSpec = new PmServerSpec();
                pmServerSpec.setId(Long.valueOf(serverSpecId));
                pmServerSpec.setStatus(Constants.STATUS_INVALID);
                pmServerSpec.setUpdateTime(curDate);
                // pmServerSpec.setUpdateBy("xx");
                int countInfo = pmServerSpecMapper.updateByPrimaryKeySelective(pmServerSpec);
                count= count+countInfo;
            }
        }
        return  count;
    }
    public List<PmServerSpecPic> getPicList(PmServerSpecPic picParam){
        PmServerSpecPicExample example  = new PmServerSpecPicExample();
        PmServerSpecPicExample.Criteria criteria = example.createCriteria();
        if(picParam.getServerSpecId()!=null){
            criteria.andServerSpecIdEqualTo(picParam.getServerSpecId());
        }
        example.setOrderByClause(" pic_type asc , pic_seq asc ");
        List<PmServerSpecPic> picList =pmServerSpecPicMapper.selectByExample(example);
        return picList;
    }

    private void savePic(PmServerSpec serverSpec){
        //1 删除原有图片
        PmServerSpecPicExample example  = new PmServerSpecPicExample();
        PmServerSpecPicExample.Criteria criteria = example.createCriteria();
        criteria.andServerSpecIdEqualTo(serverSpec.getId());
        pmServerSpecPicMapper.deleteByExample(example);
        //2 构造图片数据
        List<PmServerSpecPic> picList= new  ArrayList<PmServerSpecPic>();
        if(!StringUtils.isBlank(serverSpec.getPicMainStr())){
            int seq=0;
            String[] picArray=serverSpec.getPicMainStr().split(";");
            PmServerSpecPic pic ;
            for(String picId :picArray) {
                pic = new PmServerSpecPic();
                pic.setServerSpecId(serverSpec.getId());
                pic.setPicType(Constants.SERVER_SPEC_PIC_TYPE_MAIN);
                seq++;
                pic.setPicSeq(seq);
                pic.setPicId(picId);
                picList.add(pic);
            }
        }
        if(!StringUtils.isBlank(serverSpec.getPicListStr())){
            int seq=0;
            String[] picArray=serverSpec.getPicListStr().split(";");
            PmServerSpecPic pic ;
            for(String picId :picArray) {
                pic = new PmServerSpecPic();
                pic.setServerSpecId(serverSpec.getId());
                pic.setPicType(Constants.SERVER_SPEC_PIC_TYPE_LIST);
                seq++;
                pic.setPicSeq(seq);
                pic.setPicId(picId);
                picList.add(pic);
            }
        }
        //3 保存图片
        if(picList!=null && picList.size()>0){
            for(PmServerSpecPic pic : picList){
                pmServerSpecPicMapper.insert(pic);
            }
        }
    }
    @Override
    public PmServerSpec getById(Long id){
        PmServerSpec  pmServerSpec =pmServerSpecMapper.selectByPrimaryKey(id);

        if(pmServerSpec!=null) {
            //获取图片
            PmServerSpecPic pic = new PmServerSpecPic();
            pic.setServerSpecId(pmServerSpec.getId());
            pmServerSpec.setPicList(getPicList(pic));
        }
        return pmServerSpec;
    }
}
